package com.sdkx.app.datalayers;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;

import com.sdkx.app.model.AirSpace;
import com.sdkx.app.model.AirSpaceGeneral;
import com.sdkx.app.model.AirSpacePoint;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import cn.creable.gridgis.display.Display;
import cn.creable.gridgis.display.LineSymbol;
import cn.creable.gridgis.geometry.LineString;
import cn.creable.gridgis.geometry.Point;

public class AirSpaceLayer extends MapDraw{
    Paint paint;
    List<AirSpace> spaces = new ArrayList<>();
    public AirSpaceLayer(Context context) {
        super(context);
        paint = new Paint();
        paint.setTextSize(25);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void draw2(Canvas canvas, Display display) {

        float zoom  = display.getDisplayTransformation().getZoom();
        if(zoom >7.27099e-04f && zoom < 1.096265e-02f) {
            Canvas oldG = display.getCanvas();
            display.setCanvas(canvas);
            for(AirSpace space : spaces){
                List<AirSpacePoint> points = space.getPoints();
                if(points.size() == 0)continue;
                Point[] points1 = new Point[points.size()];

                double xmax = 0,xmin = 0,ymax = 0,ymin = 0;
                for(int i =0; i<points.size();i++){
                    AirSpacePoint asp = points.get(i);
                   points1[i] = new Point(asp.getDoubleLongitude(),asp.getDoubleLatitude());
                   if(i == 0){
                       xmax = points1[i].getX();
                       xmin = points1[i].getX();
                       ymax = points1[i].getY();
                       ymin = points1[i].getY();
                   }else{
                       if(xmax < points1[i].getX())
                           xmax =points1[i].getX();
                       if(xmin > points1[i].getX())
                           xmin = points1[i].getX();
                       if(ymax > points1[i].getY())
                           ymax = points1[i].getY();
                       if(ymin < points1[i].getY())
                           ymin = points1[i].getY();
                   }
                }
                LineSymbol symbol = new LineSymbol();
                symbol.setColor(space.getLinecolor());
                symbol.setWidth(space.getLinewidth());
                Point result = new Point();
                Point centerlatlon = null;
                if(space.getType() == AirSpace.TYPE_POLYGON) {

                    LineString lineString = new LineString(points1.length+1,null);
                    for(int i = 0;i< points1.length;i++){
                        lineString.setPoint(i,points1[i]);
                    }
                    lineString.setPoint(points1.length,points1[0]);
                    display.DrawPolyline(lineString,symbol);
                    centerlatlon = new Point( (xmax+xmin)/2,(ymax+ymin)/2);
                }else if(space.getType() == AirSpace.TYPE_ROUND){
                    AirSpacePoint asp = points.get(0);
                    LineString lineString = getCircleLines(points1[0],asp.getRadius());
                    display.DrawPolyline(lineString,symbol);
                    centerlatlon = points1[0];
                }
                float length = paint.measureText(space.getName());
                display.getDisplayTransformation().fromMapPoint(centerlatlon,result);
                canvas.drawText(space.getName(), (float)((int)(result.getX() - length/2)), (float)((int)result.getY()) - paint.getFontMetrics().bottom - paint.getFontMetrics().top, paint);
            }
            display.setCanvas(oldG);
        }

    }

    @Override
    public void upData() {
       List<AirSpaceGeneral> generals = LitePal.findAll(AirSpaceGeneral.class);
       List<AirSpace> list = new ArrayList<>();
       for(AirSpaceGeneral general : generals){
            List<AirSpace> airSpaces =  general.getSpacelist();
            for(AirSpace airSpace :airSpaces){
                List<AirSpacePoint>  points = airSpace.getPointsIndb();
                if(points.size() == 0)continue;
                boolean flag = false;
                for(AirSpacePoint pois : points){
                    Log.e("AirspacePoint","airspacepointid = "+pois.getId());
                    if(isInScreen(pois.getDoubleLongitude(),pois.getDoubleLatitude())) {
                        flag = true;
                        break;
                    }
                }
                if(flag)
                    list.add(airSpace);
            }
        }
        synchronized (this) {
            spaces = list;
        }
    }
}
